package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description NusingProjectServiceImpl
 *
 * @author zhangyan
 * @time 2024/10/15
 */

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //创建分页组件pageHelper
        PageHelper.startPage(pageNum, pageSize);
        //查询列表创建Page对象
        Page<NursingProjectVo> page = nursingProjectMapper.getByPage(name, status);
        // 将page转换为PageResponse对象
        PageResponse<NursingProjectVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());

        return pageResponse;
    }

    @Override
    public void add(NursingProjectDto nursingProjectDTO) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDTO, nursingProject);
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public void update(NursingProjectDto nursingProjectDTO) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDTO, NursingProject.class);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProjectVo nursingProjectVo = nursingProjectMapper.getById(id);
        return nursingProjectVo;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        NursingProject nursingProject = new NursingProject();
        nursingProject.setId(id);
        nursingProject.setStatus(status);
        nursingProjectMapper.update(nursingProject);

    }

    @Override
    public void removeById(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProjectVo> getAll() {
        return  nursingProjectMapper.getAll();
    }
}
